<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-date-picker
          v-model="params['create_time.gte']"
          type="date"
          value-format="yyyy-MM-dd"
          @change="setParams"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.notice_placard_title"
          placeholder="标题名称"
          @keyup.enter.native="getData()"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>
      <el-form-item class="float-right">
        <el-button type="primary" v-if="isInside" icon="el-icon-plus" @click="editNotice()">
          新增通知
        </el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:content="scope">
        <div class="text-truncate" :title="scope.row.content">{{ scope.row.content }}</div>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>
    <panel-notice-detail :visible.sync="detailVisible" :form-data="formData"></panel-notice-detail>
    <notice-edit
      :visible.sync="editVisible"
      :form-data="editData"
      @success="getData()"
      :table-id="tableId"
    ></notice-edit>
  </div>
</template>

<script>
import PanelNoticeDetail from '@/credit/views/city/index/PanelNoticeDetail'
import NoticeEdit from '@/credit/views/city/manage/NoticeEdit'
import { dateFormat } from '@/common/utils/common'
import { mapGetters } from 'vuex'
export default {
  components: {
    NoticeEdit,
    PanelNoticeDetail,
  },

  data() {
    return {
      params: {
        index: 1,
        size: 15,
      },
      order: { create_time: 'desc' },
      tableCount: 0,
      tableName: 'notice_placard',
      loading: false,
      tableColumns: [
        {
          attrs: { label: '时间', prop: 'create_time', align: 'center', width: 180 },
          process: v => dateFormat(v, 'yyyy-MM-dd hh:mm'),
        },
        { attrs: { label: '标题', prop: 'notice_placard_title' } },
        {
          attrs: { label: '操作', align: 'center', width: 150 },
          operations: [
            { content: '详情', icon: 'fa fa-search', event: this.handleView },
            {
              content: '修改',
              icon: 'fa fa-edit',
              event: this.editNotice,
              ifRender: row => row.service_id === this.clientId && this.isInside,
            },
            {
              content: '删除',
              icon: 'fa fa-trash',
              attrs: { class: 'text-danger' },
              event: this.deleteNotice,
              ifRender: row => row.service_id === this.clientId && this.isInside,
            },
          ],
        },
      ],
      tableData: [],
      detailVisible: false,
      formData: {},

      editData: {},
      tableId: '',
      editVisible: false,
    }
  },

  computed: {
    ...mapGetters(['clientId', 'user']),
    isInside() {
      return this.user.scopeArr.includes('inside')
    },
  },

  created() {
    this.params['orWhere.service_id.eq'] = [this.clientId, 'all']
    this.getData()
  },
  methods: {
    setParams(date) {
      if (date) {
        this.params['create_time.lte'] = date + ' 23:59:59'
      } else {
        this.params['create_time.lte'] = ''
      }
    },
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      this.$api
        .getData('credit', this.tableName, this.params, this.order)
        .then(res => {
          this.tableData = (res && res.data && res.data.data) || []
          this.tableCount = (res && res.data && res.data.totalCount) || 0
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleView(p) {
      this.detailVisible = true
      this.formData = p
    },

    editNotice(row) {
      this.editData = row ? { ...row } : {}
      this.tableId = row ? row.notice_placard_id : ''
      this.editVisible = true
    },
    deleteNotice(row) {
      this.$utils
        .msgbox({
          message: `确定删除：${row.notice_placard_title}?`,
          beforeClose: () =>
            this.$api.deleteData(this.$serviceName, this.tableName, row.notice_placard_id),
        })
        .then(() => {
          this.$message.success('删除成功！')
          this.getData()
        })
        .catch(() => this.$message.error('删除失败，请重试！'))
    },
  },
}
</script>
<style lang="scss" scoped></style>
